home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / nis.c < prev    next >
C/C++ Source or Header  |  1996-03-09  |  3KB  |  158 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <netdb.h>
  6. #include "netconf.h"
  7. #include "../misc/misc.h"
  8. #include "../userconf/userconf.h"
  9. #include "../xconf/xconf.h"
  10. #include "netconf.m"
  11.  
  12. static const char NIS[] = "nis";
  13. static const char NIS_DOMAIN[] = "domain";
  14. static const char NIS_SERVER[] = "server";
  15.  
  16. static NETCONF_HELP_FILE help_nis ("nis");
  17.  
  18. /*
  19.     Manage the file /etc/conf.nis
  20.     The construtor read the file. A missing file means that NIS
  21.     is not configured.
  22. */
  23. PUBLIC NIS_CONF::NIS_CONF()
  24. {
  25.     domain.setfrom (linuxconf_getval(NIS,NIS_DOMAIN));
  26.     server.setfrom (linuxconf_getval(NIS,NIS_SERVER));
  27. }
  28.  
  29. /*
  30.     Check that the server is valid.
  31.     The NIS server specification is optionnal: If the field is
  32.     empty, this is ok.
  33.  
  34.     Signal an error message if not ok.
  35.  
  36.     Return != 0 if ok.
  37. */
  38. PUBLIC int NIS_CONF::valid_server()
  39. {
  40.     int ret = 0;
  41.     if (!server.is_empty()){
  42.         const char *serverp = server.get();
  43.         if (net_isipnum(serverp)){
  44.             ret = 1;
  45.         }else if (gethostbyname(serverp) == NULL){
  46.             xconf_error (MSG_U(E_NISSERV,"Unknown NIS server %s\n"),serverp);
  47.         }else{
  48.             ret = 1;
  49.         }
  50.     }else{
  51.         ret = 1;
  52.     }
  53.     return ret;
  54. }
  55.  
  56. /*
  57.     Tells if NIS is properly configured
  58. */
  59. PUBLIC int NIS_CONF::configok()
  60. {
  61.     int ret = 0;
  62.     if (!domain.is_empty()){
  63.         ret = valid_server();
  64.     }
  65.     return ret;
  66. }
  67.  
  68. /*
  69.     Return the name of the optionnal NIS server or NULL.
  70. */
  71. PUBLIC const char *NIS_CONF::getserver()
  72. {
  73.     return server.is_empty() ? (const char *)NULL : server.get();
  74. }
  75.  
  76. /*
  77.     Return the NIS domain name (Unrelated to the BIND domain name).
  78. */
  79. PUBLIC const char *NIS_CONF::getdomain()
  80. {
  81.     return domain.get();
  82. }
  83.  
  84. PUBLIC void NIS_CONF::write ()
  85. {
  86.     linuxconf_replace(NIS,NIS_DOMAIN,domain);
  87.     linuxconf_replace(NIS,NIS_SERVER,server);
  88.     linuxconf_save();
  89. }
  90.  
  91. /*
  92.     Edit the configuration of the NIS client
  93.     Return 0 if the edition was successful.
  94. */
  95. PUBLIC int NIS_CONF::edit ()
  96. {
  97.     DIALOG dia;
  98.     dia.newf_str (MSG_U(F_NISDOM,"NIS domain"),domain);
  99.     dia.newf_str (MSG_U(F_NISSERV,"NIS server(optionnal)"),server);
  100.     int ret = -1;
  101.     while (1){
  102.         if (dia.edit(MSG_U(T_NISCONF,"NIS client configuration")
  103.             ,MSG_U(I_NISCONF,"You must enter the NIS domain\n"
  104.              "A server must be specified\n"
  105.              "if it can't be probed by a broadcast\n"
  106.              "The server is either specified as an IP number\n"
  107.              "or as a name.\n")
  108.             ,help_nis.getpath()
  109.             ,0) == MENU_ACCEPT){
  110.             if (valid_server()){
  111.                 ret = 0;
  112.                 break;
  113.             }
  114.         }else{
  115.             break;
  116.         }
  117.     }
  118.     if (ret != 0) dia.restore();
  119.     return ret;
  120. }
  121.  
  122. /*
  123.     Edite the file /etc/hosts
  124. */
  125. void netconf_editnis()
  126. {
  127.     NIS_CONF nis;
  128.     if (nis.edit() == 0) nis.write();
  129. }
  130. /*
  131.     Clone of the /bin/domainname command.
  132. */
  133. int netconf_domainname (int argc, char *argv[])
  134. {
  135.     /* #Specification: netconf / aliases / domainname
  136.         netconf emulate the command /bin/domainname. Without argument
  137.         it print the domainname (used by NIS). With one, it set the
  138.         domain name.
  139.     */
  140.     int ret = -1;
  141.     if (argc == 1){
  142.         char buf[100];
  143.         if (getdomainname(buf,sizeof(buf)-1)==-1) strcpy (buf,"none");
  144.         ret = 0;
  145.         printf ("%s\n",buf);
  146.     }else if (argc == 2){
  147.         if (perm_rootaccess (MSG_U(P_SETTINGNIS
  148.             ,"Setting the NIS domain name"))){
  149.             ret = setdomainname(argv[1],strlen(argv[1])+1);
  150.         }
  151.     }else{
  152.         fprintf (stderr,MSG_U(E_USAGEDOM
  153.             ,"usage: domainname [ nisdomain ]\n"));
  154.     }
  155.     return ret;
  156. }
  157.  
  158.